#include "StateExample.h"

#include "StringUtility.h"

StateExample::StateExample() : GameBaseState()
{
    // Constructor, set up static game variables here
    testText.loadFont("fonts/unispace.ttf",24);
}

StateExample::~StateExample()
{
    // Destructor, deallocate pointers here
}

void StateExample::init()
{
    // init function, set up variable game vars here (such as timers)
    // This will be called right before the state is activated (the constructor might be called way before that)
    timer.start(3000);
}


void StateExample::onPause()
{
    // Prepare game for being paused
    // Pause all running timers for example
    timer.pause();
}

void StateExample::onResume()
{
    // Wake up game state from being paused
    // Resume all paused timers for example
    timer.unpause();
}

void StateExample::userInput()
{
    // Handle all user input
    // Use SimpleJoy* BaseState::input variable
    // for example:
    // if (input->isStart()) pauseToggle();

    // This updates the input (checks for pressed keys)
    // If you don't call it new key presses and released keys will not be registered (can be used to "freeze" input)
    input->update();

    // This processes global input (Start to pause for example)
    // Keep this function call right after you updated the input!
    GameBaseState::userInput();

    // Custom implementation basically begins here
    if (input->isA())
    {
        timer.start();
        score += 100;
    }
    else if (input->isB())
    {
        if (not checkLives(--lives))
            loseGame();
    }

    // This resets the pressed state of keys
    // You should use it on buttons where it's critical the  associated action only gets triggeres once
    // Direction keys (to move for example) should not lead to a call of resetKey() on the other hand as
    // keeping a button pressed will not lead to a continued movement then
    input->resetKeys();
}

void StateExample::update()
{
    // Update the game
    // This is called periodically before render()
    timer.update();

    if (timer.hasFinished())
        winGame();      // this marks the current level as beaten and skips to the next one
}

void StateExample::render()
{
    // Render the game to the screen
    // Is called after update()
    GFX::clearScreen();     // this will clear the whole screen and should always be called first

    testText.setPosition(50,50);
    testText.print(StringUtility::intToString(timer.getTimeLeft()));
    testText.setPosition(50,200);
    testText.print("Lives: " + StringUtility::intToString(lives));
}
